<template>
	<view class="mainpadding2">
		<u-navbar title="優惠卡管理" placeholder autoBack>
			<view class="flexright xiaohui" slot="right" @click="show=true">添加</view>
		</u-navbar>
		<view class="flexcenter" v-if="!list.length">
			<view class="placeholderimage" style="margin-top: 100rpx;"></view>
		</view>
		<view class="mainpadding_top ffffff radius" v-for="item in list" :key="item.id">
			<view class="flexbetween">
				<view class="titletext xiaohuang bold">{{item.cardno}}</view>
				<!-- <view class="xiaolan">{{item.hotelcardtype.name}}</view> -->
			</view>
			<view class="flexright margin_top">
				<view class="flexleft margin_left" @click.stop="edititem(item)">
					<u-icon name="edit-pen" color="#999" size="16"></u-icon>
					<view class="xiaohei">編輯</view>
				</view>
				<view class="flexleft margin_left" @click.stop="delitem(2)">
					<u-icon name="trash" color="#999" size="16"></u-icon>
					<view class="xiaohei">刪除</view>
				</view>
			</view>
		</view>
		<u-modal :show="show" title="優惠卡管理" confirmText="確認" confirmColor="#FF5C00" @confirm="chongzhi" cancelText="取消"
			@cancel="show=false" showCancelButton>
			<view class="slot-content" style="width: 600rpx;">
				<view class="flexbetween margin_top" @click="jumpurl('/pages/workbench/youhuiklx?type=1')">
					<view class="main_size bold hei_text" style="min-width: 200rpx;">優惠卡類型：</view>
					<view class="flexright">
						<input type="text" class="textright" placeholder="請選擇" disabled v-model="hotelcard_type_name">
						<u-icon name="arrow-right" color="#999" size="16"></u-icon>
					</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="main_size bold hei_text">名稱：</view>
					<input type="number" class="textright" placeholder="請输入" v-model="cardno">
				</view>
			</view>
		</u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				show: false,
				cardno: "",
				hotelcard_type_id:"",
				hotelcard_type_name:"",
				editid: ""
			}
		},
		onLoad() {
			this.getqyList()
			uni.$on("checkyhklx", data => {
				this.hotelcard_type_name = data.name
				this.hotelcard_type_id = data.id
			})
		},
		onUnload() {
			uni.$off("checkyhklx")
		},
		methods: {
			getqyList() {
				this.http.request("/api/hotel/hotelcardIndex", 'GET', {
					shop_id: uni.getStorageSync("shop_id")
				}).then(res => {
					this.list = res.data
				})
			},
			chongzhi() {
				if (this.hotelcard_type_id == "") {
					this.http.toast("請選擇優惠卡類型")
					return false
				}
				if (this.cardno == "") {
					this.http.toast("請輸入名稱")
					return false
				}
				let url = "/api/hotel/addHotelcard"
				let data = {
					shop_id: uni.getStorageSync("shop_id"),
					cardno: this.cardno,
					hotelcard_type_id:this.hotelcard_type_id
				}
				if (this.editid) {
					url = "/api/hotel/editHotelcard"
					this.$set(data, "id", this.editid)
				}
				this.http.request(url, 'GET', data).then(res => {
					this.http.toast(res.msg)
					if (res.code == 1) {
						this.show = false
						this.name = ""
						this.cardno = ""
						this.hotelcard_type_id = ""
						this.hotelcard_type_name = ""
						this.editid = ""
						this.getqyList()
					}
				})
			},
			edititem(item) {
				this.editid = item.id
				this.cardno = item.cardno
				this.hotelcard_type_id = item.hotelcard_type_id
				this.hotelcard_type_name = item.hotelcardtype.name
				this.show = true
			},
			delitem(id) {
				this.http.modal('提示', '確認刪除當前數據嗎？', true, (res) => {
					if (res) {
						this.http.request("/api/hotel/delHotelcardType", 'GET', {
							id
						}).then(res => {
							if (res.code == 1) {
								this.http.toast("刪除成功")
								this.getqyList()
							} else {
								this.http.toast(res.msg);
							}
						})

					}
				})
			},
		}
	}
</script>

<style>

</style>